package com.eastfair.home.service.impl;

//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.home.dao.ReportEarnestMoneyAuditHistoryMapper;
import com.eastfair.home.dto.ReportEarnestMoneyAuditHistoryDTO;
import com.eastfair.home.dto.ReportSpaceAuditDTO;
import com.eastfair.home.entity.ReportEarnestMoneyAudit;
import com.eastfair.home.entity.ReportEarnestMoneyAuditHistory;
import com.eastfair.home.service.ReportEarnestMoneyAuditHistoryService;
import com.eastfair.home.vo.ReportEarnestMoneyAuditHistoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 报馆保证金审核记录历史
 * </p>
 *
 * @author linan
 * @date 2023-04-17
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportEarnestMoneyAuditHistoryServiceImpl extends SuperServiceImpl<ReportEarnestMoneyAuditHistoryMapper, ReportEarnestMoneyAuditHistory> implements ReportEarnestMoneyAuditHistoryService {

    @Resource(name = "threadPoolTaskExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportEarnestMoneyAuditHistory> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportEarnestMoneyAuditHistory model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<ReportEarnestMoneyAuditHistoryVO> queryReportEarnestMoneyAuditHistory(ReportEarnestMoneyAuditHistoryDTO reportManageAuditHistoryDTO) {
        LambdaQueryWrapper<ReportEarnestMoneyAuditHistory> wrapper = new QueryWrapper<ReportEarnestMoneyAuditHistory>().lambda()
                .eq(ReportEarnestMoneyAuditHistory::getIsDeleted, BusinessConstant.NO)
                .eq(ReportEarnestMoneyAuditHistory::getReportSpaceId, reportManageAuditHistoryDTO.getReportSpaceId())
                .orderByDesc(ReportEarnestMoneyAuditHistory::getAuditTime);
        List<ReportEarnestMoneyAuditHistory> list = list(wrapper);
        return ConvertUtil.convertList(list, ReportEarnestMoneyAuditHistoryVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveHistoryByReportSpaceAudit(ReportSpaceAuditDTO reportSpaceAuditDTO, LocalDateTime submitTime) {
        // 插入审核记录
        ReportEarnestMoneyAuditHistory reportEarnestMoneyAuditHistory = new ReportEarnestMoneyAuditHistory();
        reportEarnestMoneyAuditHistory.setAuditState(reportSpaceAuditDTO.getEarnestMoneyAuditStatus().getCode());
        reportEarnestMoneyAuditHistory.setAuditOpinion(reportSpaceAuditDTO.getEarnestMoneyAuditOpinion());
        reportEarnestMoneyAuditHistory.setAuditTime(LocalDateTime.now());
        reportEarnestMoneyAuditHistory.setAuditUserId(ContextUtil.getUserId());
        reportEarnestMoneyAuditHistory.setAuditUserName(ContextUtil.getRealName());
        reportEarnestMoneyAuditHistory.setReportSpaceAuditId(reportSpaceAuditDTO.getId());
        reportEarnestMoneyAuditHistory.setReportSpaceId(reportSpaceAuditDTO.getReportSpaceId());
        reportEarnestMoneyAuditHistory.setSubmitTime(submitTime);
        return save(reportEarnestMoneyAuditHistory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveHistoryByReportEarnestMoneyAudit(ReportEarnestMoneyAudit reportEarnestMoneyAudit) {
        // 插入审核记录
        ReportEarnestMoneyAuditHistory reportEarnestMoneyAuditHistory = new ReportEarnestMoneyAuditHistory();
        reportEarnestMoneyAuditHistory.setAuditState(reportEarnestMoneyAudit.getAuditState());
        reportEarnestMoneyAuditHistory.setAuditOpinion(reportEarnestMoneyAudit.getAuditOpinion());
        reportEarnestMoneyAuditHistory.setAuditTime(LocalDateTime.now());
        reportEarnestMoneyAuditHistory.setAuditUserId(ContextUtil.getUserId());
        reportEarnestMoneyAuditHistory.setAuditUserName(ContextUtil.getRealName());
        reportEarnestMoneyAuditHistory.setReportSpaceAuditId(reportEarnestMoneyAudit.getReportSpaceAuditId());
        reportEarnestMoneyAuditHistory.setReportSpaceId(reportEarnestMoneyAudit.getReportSpaceId());
        reportEarnestMoneyAuditHistory.setSubmitTime(reportEarnestMoneyAudit.getCreateTime());
        return save(reportEarnestMoneyAuditHistory);
    }

    @Override
    public void asyncSaveHistoryByReportEarnestMoneyAudit(ReportEarnestMoneyAudit reportEarnestMoneyAudit) {
        threadPoolTaskExecutor.execute(() -> saveHistoryByReportEarnestMoneyAudit(reportEarnestMoneyAudit));
    }
}
